package com.graduation.sports.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.graduation.sports.R;
import com.graduation.sports.bean.CommunityInfo;
import com.graduation.sports.bean.GroupInfo;
import com.graduation.sports.listener.OnItemClickListener;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class CommunityAdapter extends RecyclerView.Adapter<CommunityAdapter.CommunityViewHolder>{
    private Context mContext;
    private List<CommunityInfo> mListData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public CommunityAdapter(Context mContext, List<CommunityInfo> mListData) {
        //  mListData = new ArrayList<>();
        this.mContext = mContext;
        this.mListData = mListData;
    }

    public void update(List<CommunityInfo> list){
        if(list != null && list.size() > 0){
            mListData = list;
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public CommunityViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(mContext == null){
            mContext = parent.getContext();
        }
        View view  = LayoutInflater.from(mContext).inflate(R.layout.layout_item_community,parent,false);
        return new CommunityViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommunityViewHolder holder, int position) {
        CommunityInfo communityInfo = mListData.get(position);
        if (communityInfo == null) {
            return;
        }
        holder.tvName.setText(communityInfo.getUserName());
        holder.content.setText(communityInfo.getContent());
        holder.tvDate.setText(communityInfo.getTimeStamp());
    }

    @Override
    public int getItemCount() {
        return  mListData.isEmpty() ? 0 : mListData.size();
    }

    class CommunityViewHolder extends RecyclerView.ViewHolder{
        TextView content;
        TextView tvName;
        TextView tvDate;
        public CommunityViewHolder(@NonNull View itemView) {
            super(itemView);
            content =  itemView.findViewById(R.id.item_community_content);
            tvName =  itemView.findViewById(R.id.item_community_name);
            tvDate =  itemView.findViewById(R.id.item_community_date);
        }
    }
}
